import os
import sys

# 请在此输入您的代码

n,m = map(int,input().split())

data = []

for _ in range(n):
  data.append(list(map(int,input().split())))

cache = [[-1] * m for _ in range(n)]

def dfs(x,y):
  if cache[x][y] != -1:
    return cache[x][y]
  
  ans = 1
  dx = [-1,0,1,0]
  dy = [0,-1,0,1]
  for i in range(len(dx)):
    xx = dx[i] + x
    yy = dy[i] + y
    if 0 <= xx < n and 0<= yy < m and data[xx][yy] < data[x][y]:
      ans = max(dfs(xx,yy)+1,ans)
    cache[x][y] = ans
  return ans



res = 0

for i in range(n):
  for j in range(m):
    res = max(dfs(i,j),res)

print(res)


